package com.alex.statistics.controller;

import com.alex.statistics.method.explorationAnalysis.DBSCANDetector;
import com.alex.statistics.method.explorationAnalysis.VariableRelationAnalyzer;
import com.alex.statistics.method.explorationAnalysis.ZScoreMultiAnalyzer;
import com.alex.statistics.pojo.*;
import com.alex.statistics.pojo.request.explorationAnalysis.VariableRelationRequest;
import com.alex.statistics.pojo.request.explorationAnalysis.DBSCANRequest;
import com.alex.statistics.pojo.request.explorationAnalysis.ZScoreRequest;
import com.alex.statistics.pojo.result.explorationAnalysis.VariableRelationResult;
import com.alex.statistics.pojo.result.explorationAnalysis.DBSCANResult;
import com.alex.statistics.pojo.result.explorationAnalysis.ZScoreResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Tag(name = "Exploratory Analysis API", description = "探索性分析接口")
@RequestMapping("/exploratory")
@RestController
public class ExploratoryAnalysisController {
    @Autowired
    VariableRelationAnalyzer variableRelationAnalyzer;
    @Autowired
    ZScoreMultiAnalyzer zScoreAnalyzer;
    @Autowired
    DBSCANDetector dbscanDetector;



    @Operation(summary = "分析变量关系",
            description = "计算两个变量的协方差和相关系数")
    @PostMapping("/variable-relation")
    public Result<VariableRelationResult> analyzeVariableRelation(
            @RequestBody VariableRelationRequest request) {

        double covariance = variableRelationAnalyzer.calculateCovariance(
                request.getXData(), request.getYData());

        double correlation = variableRelationAnalyzer.calculateCorrelation(
                request.getXData(), request.getYData());

        VariableRelationResult result = new VariableRelationResult();
        result.setCovariance(covariance);
        result.setCorrelation(correlation);
        result.setStrengthDescription(getCorrelationStrength(correlation));

        return Result.ok(result);
    }

    private String getCorrelationStrength(double correlation) {
        double absCorrelation = Math.abs(correlation);
        if (absCorrelation >= 0.8) {
            return "极强相关";
        } else if (absCorrelation >= 0.6) {
            return "强相关";
        } else if (absCorrelation >= 0.4) {
            return "中等相关";
        } else if (absCorrelation >= 0.2) {
            return "弱相关";
        } else {
            return "极弱相关或无相关";
        }
    }

    @Operation(summary = "执行Z-Score异常检测")
    @PostMapping("/zscore")
    public Result<ZScoreResult> detectAnomalies(
            @RequestBody @Valid ZScoreRequest request) {
        return Result.ok(zScoreAnalyzer.detect(request));
    }


    @Operation(summary = "多维聚类异常检测")
    @PostMapping("/dbscan")

    public Result<DBSCANResult> detectAnomalies(@RequestBody DBSCANRequest request) {
        return Result.ok(dbscanDetector.detect(request));
    }
}